# -*- coding: UTF-8 -*-
# cython: language_level=3
import socket
from concurrent import futures
from datetime import datetime

from tqdm import tqdm

from base.base_parser import fenlei
from db.mysqldb import MysqlDB
from utils.get_domain_info import get_website_info
from utils.ip_area import find_ip

socket.setdefaulttimeout(3.1)

mysql_client = MysqlDB()


def save_icp(oid, old_site_type, old_host, home_page, host, title, ip, icp, video_license, visit_status,
             *args, **kwargs):
    try:
        assert ip
        country, province_name, city_name, isp = find_ip(ip)
        if country and country != '中国':
            province_name = country + province_name
    except:
        province_name = ''
        city_name = ''

    if host.endswith('.gov.cn') or host.endswith('.edu.cn') or host.endswith('.gov') or host.endswith('.edu'):
        status = 8
    else:
        if visit_status == 0:
            status = fenlei(title, icp, province_name=province_name)
        else:
            status = 3

    if old_site_type == 3 or status == 0:
        site_type = 3
    else:
        site_type = 300

    if host != old_host:
        sql = ('INSERT INTO TAB_VIDEO_SITE_INFO ('
               'SITE_NAME,PLAY_NAME,PLAY_NAME_NOW,URL,SITE_TYPE,AUTO_RECORD_NUMBER,AUDIO_LICENSE,IP_ADDRESS,'
               f'CITY_NAME,PROVINCE_NAME,FIND_DATE,DOMAIN,NO_LICENSE_SITE_CATEGORY,NO_LICENSE_SITE_STATUS,CHECK_TYPE,ADD_TYPE) VALUES ({",".join("%s" for i in range(16))})')
        assert mysql_client.add(sql, (
            title, title, title, home_page, site_type, icp, video_license, ip, city_name, province_name, datetime.now(),
            host, status, 0, 10, 1))
        sql = ('UPDATE TAB_VIDEO_SITE_INFO SET PLAY_NAME=%s,PLAY_NAME_NOW=%s,'
               'SITE_TYPE=%s '
               'WHERE ID=%s')
        mysql_client.update(sql, ('网址已迁移', '网址已迁移', 303, oid))

    else:
        t = ' ' if visit_status else ',LATEST_VISIT_TIME=UNIX_TIMESTAMP(NOW()) '
        sql = ('UPDATE TAB_VIDEO_SITE_INFO SET '
               'PLAY_NAME_NOW=%s,'
               'URL=%s,'
               'NEW_IP_ADDRESS=%s,'
               'PROVINCE_NAME=%s,'
               'CITY_NAME=%s,'
               'NEW_AUTO_RECORD_NUMBER=%s,'
               'AUDIO_LICENSE=%s,'
               'VISIT_STATUS=%s,'
               'SITE_TYPE=%s,'
               'NO_LICENSE_SITE_CATEGORY=%s' + t + 'WHERE ID=%s')
        mysql_client.update(sql, (title, home_page, ip, province_name,
                                  city_name, icp, video_license, visit_status, site_type, status, oid))


def add_root_url(home_page, host, oid, site_type):
    data = get_website_info(home_page, host=host)
    # {"home_page": home_page, "host": host, "title": title, "ip": ip, "icp": icp, "video_license": video_license,
    #  "visit_status": visit_status}
    save_icp(oid, site_type, host, **data)


def add_root_url_t(INPUT_URLS, n=None):
    len_urls = len(INPUT_URLS)
    p_num = min(len_urls, 40)
    if not p_num:
        return

    tasks, results = [], []
    with futures.ThreadPoolExecutor(max_workers=p_num) as executor:
        for oid, url, host, site_type in tqdm(INPUT_URLS, total=len_urls, desc='add_task'):
            tasks.append(executor.submit(add_root_url, url, host, oid, site_type))

        for task in tqdm(futures.as_completed(tasks), total=len_urls, desc=f'update_mongo-{n}'):
            # results.append(task.result())
            pass
    # print(results)


def check_main(only_null=True, max_workers=10):
    sql = "select ID,URL,DOMAIN,SITE_TYPE from TAB_VIDEO_SITE_INFO where SITE_TYPE in (3,300)"
    if only_null:
        sql = sql + " and (PLAY_NAME_NOW is null or PLAY_NAME_NOW='')"
    INPUT_URLS = mysql_client.find(sql)
    len_urls = len(INPUT_URLS)
    print('添加url数量： ', len_urls, 'max_workers:', max_workers)
    if not len_urls:
        return

    step = int(len_urls / max_workers) + 1
    INPUT_URLS = [INPUT_URLS[i:i + step] for i in range(0, len_urls, step)]

    tasks, results = [], []
    with futures.ProcessPoolExecutor(max_workers=max_workers) as executor:
        for i, data in enumerate(INPUT_URLS):
            tasks.append(executor.submit(add_root_url_t, data, i))
        for task in tqdm(futures.as_completed(tasks), total=max_workers, desc='总进度'):
            # results.append(task.result())
            pass
    # print(results)


if __name__ == "__main__":
    # check_main(only_null=True)
    check_main()

    # print('start screenshot...')
    # from utils.screenshot import screen_shot
    #
    # screen_shot()
